package com.htpay.controller;

import com.htpay.aspect.AutoLog;
import com.htpay.model.common.ResponseBean;
import com.htpay.model.entity.Merchant;
import com.htpay.model.entity.MerchantAuth;
import com.htpay.model.entity.MerchantWalletInfo;
import com.htpay.model.vo.IndexModel;
import com.htpay.service.IMerchantAuthService;
import com.htpay.service.IMerchantService;
import com.htpay.service.IMerchantWalletService;
import com.htpay.util.MerchantUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 首页相关的、以及其他部分查询
 */
@Slf4j
@RestController
@RequestMapping("/sys/info")
public class IndexController {

    @Autowired
    IMerchantService merchantService;

    @Autowired
    IMerchantWalletService merchantWalletService;

    @Autowired
    IMerchantAuthService merchantAuthService;

    @AutoLog(value = "首页接口查询")
    @GetMapping(value = "/index")
    public ResponseBean indexInfo(HttpServletRequest req) {
        ResponseBean bean = new ResponseBean();
        Integer merchantId = MerchantUtil.getMerchantId(req);

        try {
            Merchant merchant = merchantService.getById(merchantId);
            if (merchant == null) {
                return bean.error("该用户不存在");
            }

            //获取该商户的余额
            MerchantWalletInfo walletInfo = merchantWalletService.getWalletInfoByMerchantId(merchantId);
            //认证信息
            MerchantAuth auth = merchantAuthService.findMerchantAuthInfo(merchantId);

            //首页数据
            IndexModel model = new IndexModel();
            model.setMerchantId(merchantId);
            model.setUserName(merchant.getLoginName());
            model.setEmail(merchant.getEmail());
            model.setPhone(merchant.getPhone());
            if (auth != null) model.setName(auth.getName());
            model.setTotalMoney(walletInfo.getMoney());
            model.setFreezeMoney(walletInfo.getFreezeMoney());

            return bean.success("查询成功", model);
        } catch (Exception e) {
            log.info(e.getMessage());
            return bean.error(e.getMessage());
        }

    }

}
